//
//  XiaoKaiUserCollectionViewCell.m
//  Project_A
//
//  Created by 凯 on 15/7/21.
//  Copyright (c) 2015年 zhoukaidong. All rights reserved.
//

#import "XiaoKaiUserCollectionViewCell.h"
#import "BaseHeader.h"
#import "XiaoKaiFirstCollectionViewCell.h"
#import "XiaoKaiSecondCollectionViewCell.h"
#import "DataBase.h"

@interface XiaoKaiUserCollectionViewCell ()

@property(nonatomic,assign)NSInteger count;
@property(nonatomic,assign)CGFloat height;
@property(nonatomic,assign)BOOL isReload;
@end

@implementation XiaoKaiUserCollectionViewCell

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.autoresizingMask = NO;
        [self createCollectionViewWithFrame:frame];
    }
    return self;
}



- (void)createCollectionViewWithFrame:(CGRect)frame
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.minimumLineSpacing = 0;
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.collection = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height) collectionViewLayout:layout];
    self.collection.backgroundColor = [UIColor whiteColor];
    self.collection.pagingEnabled = YES;
    self.collection.bounces = YES;
    self.collection.delegate = self;
    self.collection.dataSource = self;
    [self.collection registerClass:[XiaoKaiFirstCollectionViewCell class] forCellWithReuseIdentifier:@"cell1"];
    [self.collection registerClass:[XiaoKaiSecondCollectionViewCell class] forCellWithReuseIdentifier:@"cell2"];
    [self.contentView addSubview:self.collection];
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.section == 0) {
        
        return CGSizeMake(SCREEN_WIDTH, _arrLeft.count * SCREEN_HEIGHT / 4);
     }else{
        if (_arrRight.count % 2 == 0) {
            return CGSizeMake(SCREEN_WIDTH, _arrRight.count / 2 * 205 + 20);
        } else {
            return CGSizeMake(SCREEN_WIDTH, _arrRight.count / 2 * 205 + 205 + 20);
        }
    }

//    if (count == 0 ) {
//        return CGSizeMake(SCREEN_WIDTH, self.contentView.frame.size.height);
//    }else{
//        return CGSizeMake(SCREEN_WIDTH, _height);
//    }
//    return CGSizeMake(SCREEN_WIDTH, self.contentView.frame.size.height);

}


-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 1;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
//    if (indexPath.section == 0) {
        XiaoKaiFirstCollectionViewCell *cell1 = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell1" forIndexPath:indexPath];
        
        cell1.dataSource = _arrLeft;
        cell1.tableview.frame = CGRectMake(0, 0, SCREEN_WIDTH, self.contentView.frame.size.height);
//        _height = cell.contentView.frame.size.height;
//        if (count != 0 ) {
//            if (!_isReload) {
//                [self.collection reloadData];
//                _isReload  = YES;
//            }
//        }
        NSLog(@"~~~~~~~~~~~~~~~%f",cell1.contentView.frame.size.height);
//        [cell.tableview reloadData];
        return cell1;
//    }else {
//        XiaoKaiSecondCollectionViewCell *cell2 = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell2" forIndexPath:indexPath];
////        cell2.collectionView.frame = CGRectMake(0, 0, cell.contentView.frame.size.width, cell.contentView.frame.size.height);
//        cell2.dataSource = _arrRight;
//        return cell2;
//    }
}


static NSInteger count = 0;


-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    CGFloat x = scrollView.contentOffset.x;
//
//    
////    NSLog(@"%f",SCREEN_WIDTH);
////    _isReload = NO;
//    count ++;
////    if (x == 0 || x == SCREEN_WIDTH) {
////        self.block(x);
////        
////    }

    if (x == 0) {
        self.collection.frame = CGRectMake(0, 0, SCREEN_HEIGHT, _arrLeft.count * SCREEN_HEIGHT / 4);
    }else{
        if (_arrRight.count % 2 == 0) {
            self.collection.frame = CGRectMake(0, 0, SCREEN_HEIGHT, _arrRight.count / 2 * 205 + 20);
        } else {
            self.collection.frame = CGRectMake(0, 0, SCREEN_HEIGHT, _arrRight.count / 2 * 205 + 205 + 20);
        }
    }
    
//    [self.collection reloadData];
}


@end
